package com.lanyou.cook.web;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.entity.domain.Field;
import com.lanyou.cook.entity.domain.FieldSystem;
import com.lanyou.cook.entity.domain.InterfaceSchedule;
import com.lanyou.cook.entity.domain.SystemInterface;
import com.lanyou.cook.entity.domain.userManagement.User;
import com.lanyou.cook.entity.kettle.KettleJob;
import com.lanyou.cook.entitylistener.domain.FieldEntityListenerForKettleObject;
import com.lanyou.cook.entitylistener.domain.FieldSystemEntityListenerForKettleObject;
import com.lanyou.cook.entitylistener.domain.UserEntityListenerForKettleObject;
import com.lanyou.cook.repository.FieldRepository;
import com.lanyou.cook.repository.FieldSystemRepository;
import com.lanyou.cook.repository.InterfaceScheduleRepository;
import com.lanyou.cook.repository.SystemInterfaceRepository;
import com.lanyou.cook.repository.userManagement.UserRepository;
import com.lanyou.cook.service.InterfaceScheduleService;

/**
 * kettle表的数据维护相关业务的控制层
 * @author 仇伟业
 *
 */
@RestController
@RequestMapping(Constants.BASE_URL_API + "/dataMaintenance")
public class DataMaintenanceController {

	@Autowired
	private FieldRepository fieldRepository;
	@Autowired
	private FieldSystemRepository fieldSystemRepository;
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private SystemInterfaceRepository systemInterfaceRepository;
	@Autowired
	private InterfaceScheduleRepository interfaceScheduleRepository;
	@Autowired
	private InterfaceScheduleService interfaceScheduleService;

	/**
	 * 维护kettel目标表的数据
	 */
	@RequestMapping(value = "/fixKettleDirectory",  method=RequestMethod.POST)
	@Transactional
	public void fixKettleDirectory() {
		for(Field entity : fieldRepository.findAll()){
			FieldEntityListenerForKettleObject.setKettleDirectory(entity);
			fieldRepository.saveAndFlush(entity);
		}
		
		for(FieldSystem entity : fieldSystemRepository.findAll()){
			FieldSystemEntityListenerForKettleObject.setKettleDirectory(entity);
			fieldSystemRepository.saveAndFlush(entity);
		}
	}
	
	/**
	 * 维护kettel用户表的数据
	 */
	@RequestMapping(value = "/fixKettleUser1",  method=RequestMethod.POST)
	@Transactional
	public void fixKettleUser1(){
		for(User entity : userRepository.findAll()){
			UserEntityListenerForKettleObject.setKettleUser(entity);
			userRepository.saveAndFlush(entity);
		}
	}
	
	/**
	 * 维护kettel作业表的数据
	 */
	@RequestMapping(value = "/fixKettleJob1",  method=RequestMethod.POST)
	@Transactional
	public void fixKettleJob1(){
		for(SystemInterface entity : systemInterfaceRepository.findAll()){
			KettleJob kettleJob = entity.getKettleJob();
			kettleJob.setDirectoryId(entity.getOwnerFieldSystem().getKettleDirectory().getId());
			kettleJob.setName(entity.getJobName());
			
			systemInterfaceRepository.saveAndFlush(entity);
		}
	}
	
	@RequestMapping(value = "/reloadInterfaceSchedule",  method=RequestMethod.POST)
	public void reloadInterfaceSchedule(String interfaceScheduleId) throws Exception {
		List<InterfaceSchedule> entitys;
		if(!StringUtils.isBlank(interfaceScheduleId)){
			entitys = Arrays.asList(interfaceScheduleRepository.findOne(interfaceScheduleId));
		} else {
			entitys = interfaceScheduleRepository.findAll();
		}
		for(InterfaceSchedule interfaceSchedule : entitys){
			interfaceScheduleService.notifyChanged(interfaceSchedule.getId());
		}
	}
	
}
